package com.competition.commgr.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.competition.common.annotation.Log;
import com.competition.common.core.controller.BaseController;
import com.competition.common.core.domain.AjaxResult;
import com.competition.common.enums.BusinessType;
import com.competition.commgr.domain.Competitions;
import com.competition.commgr.service.ICompetitionsService;
import com.competition.common.utils.poi.ExcelUtil;
import com.competition.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 赛事管理Controller
 * 
 * @author cui
 * @date 2025-01-21
 */
@RestController
@RequestMapping("/commgr/competitions")
public class CompetitionsController extends BaseController
{
    @Autowired
    private ICompetitionsService competitionsService;

    /**
     * 查询赛事管理列表
     */
    @PreAuthorize("@ss.hasPermi('commgr:competitions:list')")
    @GetMapping("/list")
    public TableDataInfo list(Competitions competitions)
    {
        startPage();
        List<Competitions> list = competitionsService.selectCompetitionsList(competitions);
        return getDataTable(list);
    }

    /**
     * 导出赛事管理列表
     */
    @PreAuthorize("@ss.hasPermi('commgr:competitions:export')")
    @Log(title = "赛事管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Competitions competitions)
    {
        List<Competitions> list = competitionsService.selectCompetitionsList(competitions);
        ExcelUtil<Competitions> util = new ExcelUtil<Competitions>(Competitions.class);
        util.exportExcel(response, list, "赛事管理数据");
    }

    /**
     * 获取赛事管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('commgr:competitions:query')")
    @GetMapping(value = "/{competitionId}")
    public AjaxResult getInfo(@PathVariable("competitionId") Long competitionId)
    {
        return success(competitionsService.selectCompetitionsByCompetitionId(Math.toIntExact(competitionId)));
    }

    /**
     * 新增赛事管理
     */
    @PreAuthorize("@ss.hasPermi('commgr:competitions:add')")
    @Log(title = "赛事管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Competitions competitions)
    {
        return toAjax(competitionsService.insertCompetitions(competitions));
    }

    /**
     * 修改赛事管理
     */
    @PreAuthorize("@ss.hasPermi('commgr:competitions:edit')")
    @Log(title = "赛事管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Competitions competitions)
    {
        return toAjax(competitionsService.updateCompetitions(competitions));
    }

    /**
     * 删除赛事管理
     */
    @PreAuthorize("@ss.hasPermi('commgr:competitions:remove')")
    @Log(title = "赛事管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{competitionIds}")
    public AjaxResult remove(@PathVariable Integer[] competitionIds)
    {
        return toAjax(competitionsService.deleteCompetitionsByCompetitionIds(competitionIds));
    }

    @PreAuthorize("@ss.hasPermi('commgr:competitions:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Competitions> util = new ExcelUtil<Competitions>(Competitions.class);
        List<Competitions> userList = util.importExcel(file.getInputStream());
        System.out.println(userList);
        String operName = getUsername();
        String message = competitionsService.importCompetitions(userList, updateSupport, operName);
        return success(message);
    }


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Competitions> util = new ExcelUtil<Competitions>(Competitions.class);
        util.importTemplateExcel(response, "赛事类别数据");
    }

}
